// pages/event-add/index.js
import {curTimeAll} from '../../third-utils/time/index.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      recordDate:'',
        isSymmetryAttr:['是','否'],
        isSymmetryIndex:'',
        sleepAttr:['有影响','无影响'],
        sleepIndex:'',
        curCate:'',
        imageAttr:[],
        pf_type:{},
        typeIndex:-1,
        pf_place:{},
        itching:0,
        placeIndex:-1,
        showModal:false,
        description:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e) {

        
                    
      if (e.id) {
        this.setData({
          curEventId: e.id
        })
      }
      this.setData({
        recordDate:curTimeAll,
        curCate:e.cateId
      })
          this.initData()
    },
    


    getEventInfo(id) {

      wx.request({
          url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/view',
          data: {
              id: id
          },
          method: 'get',
          success: (res) => {
              if (res.data.code == 200) {
                  this.setDefault(res.data.data.detail)
              }
          }
      })
  },
    setDefault(curData) {
      if (this.data.curEventId) {
  
        this.setData({
          recordDate: curData.recordDate,
          description: curData.description,
          imageAttr: curData.picture ? (curData.picture).split(',') : ''
        })
      }
  
      if (curData.type) {
        for (let i in this.data.pf_type.opts) {
          console.log(i,12212)
          if (this.data.pf_type.opts[i].name == curData.type) {
            this.setData({
              typeIndex: i
            })
          }
        }
      }
  
      if (curData.position) {
        for (let i in this.data.pf_place.opts) {
          if (this.data.pf_place.opts[i].name == curData.position) {
            this.setData({
              placeIndex: i
            })
          }
        }
      }
      if (curData.isSymmetry) {
        for (let i in this.data.pf_place.opts) {
          if (this.data.isSymmetryAttr[i] == curData.isSymmetry) {
            this.setData({
              isSymmetryIndex: i
            })
          }
        }
      }
      if (curData.itching) {
        this.data.itching == curData.itching
      }
      if (curData.sleep) {
        for (let i in this.data.pf_place.opts) {
          if (this.data.sleepAttr[i] == curData.sleep) {
            this.setData({
              sleepIndex: i
            })
          }
        }
      }
      
    },
  hideModal() {
    this.setData({
      showModal: false,
    })
  },
  showModal() {
    this.setData({
      showModal: true,
    })
  },
    select(e){
      this.setData({
        [e.currentTarget.dataset.key]:e.currentTarget.dataset.index
      })
    },
    pickerChangeType(e){
      console.log(e,21)
      this.setData({
        [e.currentTarget.dataset.key]:e.detail.value
      })
    },
  formSubmit(e){
    console.log(e,12122)
    if(this.data.typeIndex==-1){
      wx.showToast({
        title: '请选择皮肤类型',
        icon: 'none',
        duration: 1200
      })
      return
    }
    if(this.data.placeIndex==-1){
      wx.showToast({
        title: '请选择问题位置',
        icon: 'none',
        duration: 1200
      })
      return
    }
    let post={
      babyId:32,
      typeId:this.data.curCate,
      
    }
      post.detail={
        recordDate:this.data.recordDate,
        type:this.data.pf_type.opts[this.data.typeIndex].name,
        position:this.data.pf_place.opts[this.data.placeIndex].name,
        isSymmetry:this.data.isSymmetryAttr[this.data.isSymmetryIndex],
        itching:this.data.itching,
        sleep:this.data.sleepAttr[this.data.sleepIndex],
        description:e.detail.value.description,
        picture:String(this.data.imageAttr)
      }
      if(this.data.pf_type.opts[this.data.typeIndex].name=='其他'){
        post.detail.type=e.detail.value.otherType
      }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data:post,
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          
          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(()=>{
            wx.navigateBack()
          },1500)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
    changeCate(e){
      this.setData({
        curCate:e.currentTarget.dataset.index
      })
    },
    delImg(e){
      this.data.imageAttr.splice(e.currentTarget.dataset.index,1)
      this.setData({
        imageAttr:this.data.imageAttr
      })
    },
    addImg(){
      wx.chooseImage({
        count:9,
        success: (chooseImageRes) => {
          console.log(chooseImageRes)
          const tempFilePaths = chooseImageRes.tempFilePaths;
          tempFilePaths.some((item)=>{
            wx.uploadFile({
              url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
              filePath:item,
              fileType: 'image',
              name: 'file',
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'multipart/form-data',
              },
              success: (uploadFileRes) => {
                let res=JSON.parse(uploadFileRes.data)
                if(res.code==200){
                  this.data.imageAttr.push(res.data.imageUrl)
                  this.setData({
                    imageAttr:this.data.imageAttr
                  })
                }
              },
            });
          })
        }
      });
    },
    initData(){
        let codeAttr=['pf_type','pf_place']
        codeAttr.some(item=>{
            wx.request({
                url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
                data: {
                    code:item
                },
                method: 'get',
                success: (res) => {
                  if (res.data.code == 200) {
                    this.setData({
                        [item]:res.data.data
                    })
              this.getEventInfo(this.data.curEventId)
                  }
                }
              })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})